import { Menu } from '@/common/model/app/menu'
import { type TabPaneName } from 'element-plus'

export default function () {
  //region # 字段

  // 应用实例
  const { proxy }: any = getCurrentInstance()

  // 首页菜单
  let homeMenu: Menu

  // 菜单列表
  const menus = reactive(new Array<Menu>())

  // 活跃菜单列表
  const activeMenus: Set<Menu> = reactive(new Set<Menu>())

  // 当前菜单标识
  const currentMenuIndex = ref('0')

  //endregion

  //region # 方法

  /**
   * 菜单导航
   * @param menu - 菜单
   * */
  const navigateTo = function (menu: Menu) {
    if (menu.routerPath == '') return

    if (!activeMenus.has(menu)) {
      activeMenus.add(menu)

      proxy.$storage.pinia.keepAlive.includes.push(menu.componentName)
    }

    currentMenuIndex.value = menu.index

    proxy.$router.push(menu.routerPath)
  }

  /**
   * 菜单标签变更
   * @param tabName - 标签名称
   * */
  const menuTabChanged = function (tabName: TabPaneName): void {
    currentMenuIndex.value = tabName.toString()

    let menu: Menu | null = getActiveMenu(currentMenuIndex.value)
    activeMenus.forEach((item: Menu) => {
      if (item.index == currentMenuIndex.value) {
        menu = item
        return
      }
    })
    menu = menu == null ? homeMenu : menu

    navigateTo(menu)
  }

  /**
   * 吊销菜单
   * @param tabName -标签名称
   * */
  const deactiveMenu = function (tabName: TabPaneName): void {
    const tabNameStr: string = tabName.toString()
    const menu: Menu | null = getActiveMenu(tabNameStr)

    if (menu == null) return

    let lastMenu: Menu | null = null
    for (const activeMenu of activeMenus) {
      if (activeMenu.index == tabNameStr) break

      lastMenu = activeMenu
    }

    activeMenus.delete(menu)
    proxy.$storage.pinia.keepAlive.removeInclude(menu.componentName)

    if (lastMenu == null || currentMenuIndex.value != tabNameStr) return

    const { index } = lastMenu
    menuTabChanged(index)
  }

  /**
   * 初始化菜单
   * */
  const initMenu = function () {
    menus.push(
      new Menu('1', '身份认证管理', false, 1, 'Expand', '', '', [
        new Menu('1-1', '用户管理', true, 1, 'Menu', 'UserIndexView', '/IndexView'),
        new Menu('1-2', '角色管理', true, 2, 'Menu', '', ''),
        new Menu('1-3', '菜单管理', true, 3, 'Menu', '', ''),
        new Menu('1-4', '权限管理', true, 4, 'Menu', 'AuthorityIndexView', '/authorityList'),
        new Menu('1-5', '登录记录', true, 5, 'Menu', '', ''),
      ]),
    )

    menus.push(
      new Menu('2', '菜单样例', false, 1, 'menu', '', '', [
        new Menu('2-1', '子菜单01', true, 1, 'menu', '', ''),
        new Menu('2-2', '子菜单02', true, 2, '', '', ''),
      ]),
    )

    menus.push(
      new Menu('3', '功能样例', false, 1, 'menu', '', '', [
        new Menu('3-1', 'UI 样例', true, 1, 'ElementPlus', 'DemoListView', '/demoList'),
        new Menu('3-2', '图表样例', true, 2, 'PieChart', 'DemoEchartsView', '/demoEcharts'),
        new Menu('3-3', 'Canvas样例', true, 3, 'PictureFilled', 'CanvasView', '/demoCanvas'),
      ]),
    )
  }

  /**
   * 初始化首页菜单
   * */
  const initHomeActiveMenu = function () {
    homeMenu = new Menu('0', '首页', false, 0, '', 'home', '/home', [])
    activeMenus.add(homeMenu)
    navigateTo(homeMenu)
  }

  /**
   * 获取活跃菜单
   * @param menuIndex - 菜单索引
   * */
  const getActiveMenu = function (menuIndex: string): Menu | null {
    let menu: Menu | null = null
    for (const activeMenu of activeMenus) {
      if (activeMenu.index == menuIndex) {
        menu = activeMenu
        break
      }
    }

    return menu
  }

  //endregion

  return {
    menus,
    activeMenus,
    currentMenuIndex,

    initMenu,
    initHomeActiveMenu,
    navigateTo,
    menuTabChanged,
    deactiveMenu,
  }
}
